package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.domain.Result;
import com.ruoyi.domain.UserPeople;
import com.ruoyi.service.UserPeopleService;
import com.ruoyi.mapper.UserPeopleMapper;
import com.ruoyi.utils.JwtUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
* @author 15022
* @description 针对表【user_people】的数据库操作Service实现
* @createDate 2023-02-23 14:34:23
*/
@Service
public class UserPeopleServiceImpl extends ServiceImpl<UserPeopleMapper, UserPeople>
    implements UserPeopleService{

    @Resource
    UserPeopleMapper userPeopleMapper;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result userLogin(UserPeople userPeople) throws UnsupportedEncodingException {
        QueryWrapper queryWrapper=new QueryWrapper();
        //判断账号是否为空，不为空比对账号
        if (!StringUtils.isEmpty(userPeople.getPeopleName())){
            queryWrapper.eq("people_name", userPeople.getPeopleName());

        }

        //判断手机号是否为空，不为空比对手机号
        if (!StringUtils.isEmpty(userPeople.getPeoplePhone())){
            queryWrapper.eq("people_phone", userPeople.getPeoplePhone());
        }
        //比对密码是否正确
        queryWrapper.eq("people_password", userPeople.getPeoplePassword());
//        List<UserPeople> userPeopleList = this.baseMapper.selectList(queryWrapper);
        List<UserPeople> userPeopleList = userPeopleMapper.selectList(queryWrapper);
        //判断有没有同时满足的数据
        if (userPeopleList.size()==1){
            UserPeople userPeople1 = userPeopleList.get(0);
            Date date = new Date();
            //创建token
            String token = JwtUtil.sign(userPeople1.getPeopleId() + "", userPeople1.getPeopleName(), "yang");
            redisTemplate.opsForHash().get("token", token);
            redisTemplate.opsForHash().put("login","times",date);
            return Result.succeed(token);
        }
        if (userPeopleList.size()<1){
            return Result.err(201,"用户名/手机号或密码错误");
        }
        return Result.err(201,"数据库错误");
    }

    @Override
    public Result phoneLogin(UserPeople userPeople) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("people_phone", userPeople.getPeoplePhone());
        UserPeople userPeople1 = this.getOne(queryWrapper);
        if (userPeople1==null){
            return Result.err(202,"手机号未注册，请先注册");
        }
        String code = stringRedisTemplate.opsForValue().get("sendcode");
        if (!userPeople.getPeopleCode().equals(code)){
            return Result.err(202,"验证码错误请检查");
        }
        return Result.succeed("登陆成功");
    }

    @Override
    public Result sendcode(String peoplePhone) {
        //随机生成6位验证码
        String code="";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code+=random.nextInt(10);
        }
        System.out.println(code);
        //验证码存入redis
        stringRedisTemplate.opsForValue().set("sendcode", code, 5, TimeUnit.MINUTES);
        return Result.succeed("验证码发送成功");
    }


}




